package cn.enjoy.swagger.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @Author K
 * @Date 2020-12-05 15:41
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfiguration {

    @Bean
    public Docket authDocket() {
        return docket("授权管理", "cn.enjoy.study.modules.auth");
    }

    @Bean
    public Docket captchaDocket() {
        return docket("拼图验证码", "com.anji.captcha");
    }

    @Bean
    public Docket sysDocket() {
        return docket("系统管理", "cn.enjoy.system");
    }

    private Docket docket(String groupName, String basePackages) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackages))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(Lists.newArrayList(securityContext()))
                .securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(), apiKey1(), apiKey2()));
        // 加入到配置中
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui RESTful APIs")
                .description("swagger-bootstrap-ui")
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0")
                .build();
    }

    /**
     * 设置请求头
     */
    private ApiKey apiKey() {
        //HttpHeaders.AUTHORIZATION
        return new ApiKey("BearerToken(apiKey)", "Blade-Auth", "header");
    }

    private ApiKey apiKey1() {
        return new ApiKey("ClientInfo(apiKey)", "Authorization", "header");
    }

    private ApiKey apiKey2() {
        return new ApiKey("TenantId(apiKey)", "Tenant-Id", "header");
    }

    /**
     * 作用范围
     */
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
    }

}